﻿using _004_坦克大战_正式.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace _004_坦克大战_正式
{
    class EnemyTank:Movething
    {
        public int ChangeDirSpeed { get; set; }
        private int ChangeDirCount = 0;
        public int AttackSpeed { get; set; }
        private int AttackCount = 0;
        private Random r = new Random();
        public EnemyTank(int x, int y, int speed,Bitmap bmpDown,Bitmap bmpUp,Bitmap bmpRight,Bitmap bmpLeft)
        {
            //IsMoveing = true;
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            BitmapDown = bmpDown;
            BitmapUp = bmpUp;
            BitmapRight = bmpRight;
            BitmapLeft = bmpLeft;
            this.Dir = Direction.Down;
            AttackSpeed = 60;
            ChangeDirSpeed = 70;
        }
        public override void Update()
        {
            MoveCheck();//移动检测

            Move();

            AutoChangeDirection();

            AttackCheck();

            base.Update();
        }




        private void MoveCheck()//检查有没有超过窗体边界
        {
           
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    ChangeDirction();return;
                }
            }else if (Dir == Direction.Down)
            {
                if (Y + Speed + Height > 390)
                {
                    ChangeDirction(); return;
                }
            }else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    ChangeDirction(); return;
                }
            }else if (Dir == Direction.Right)
            {
                if (X - Speed+Width+5 > 390)
                {
                    ChangeDirction(); return;
                }
            }

            //检查有没与其他物体发生碰撞
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speed;
                    break;
                case Direction.Down:
                    rect.Y += Speed;
                    break;
                case Direction.Left:
                    rect.X -= Speed;
                    break;
                case Direction.Right:
                    rect.X += Speed;
                    break;
            }

            if (GameObjectManager.IsCollidedWall(rect) != null)
            {
                ChangeDirction();
                return;
            }
            if (GameObjectManager.IsCollidedSteel(rect) != null)
            {
                ChangeDirction();
                return;
            }
            if (GameObjectManager.IsCollidedBoss(rect))
            {
                ChangeDirction();
                return;
            }
        }
        private void AutoChangeDirection()
        {
            ChangeDirCount++;
            if (ChangeDirCount < ChangeDirSpeed) return;
            ChangeDirction();
            ChangeDirCount = 0;
        }
        private void ChangeDirction()
        {
            while (true)
            {
                Direction dir = (Direction)r.Next(0, 4);
                if (dir == Dir)
                {
                    continue;
                }
                else
                {
                    Dir = dir;
                    break;
                }
            }
            MoveCheck();
            
        }
        public void Move()
        {
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
            }



        }
        private void AttackCheck()
        {
            AttackCount++;
            if (AttackCount < AttackSpeed) return;
            Attack();
            AttackCount = 0;
        }
        private void Attack()
        {
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                    x += Width / 2;

                    break;
                case Direction.Down:
                    x += Width / 2;
                    y += Height;
                    break;
                case Direction.Left:
                    y += Height / 2;
                    break;
                case Direction.Right:
                    x += Width;
                    y += Height / 2;
                    break;
            }
            GameObjectManager.CreateBullet(x, y, Tag.EnemyTank, Dir);
        }

    }
}
